﻿using System;
using System.Windows.Media.Imaging;
using System.Windows;
using System.Linq;
using System.Concurrency;
using Caliburn.Micro;
using PicturePortfolio.ServiceReference;
using PicturePortfolio.Silverlight.Extension;
namespace PicturePortfolio.Silverlight.ViewModels
{
    public class PreviewPhotoViewModel:PropertyChangedBase,IChildWindow
    {
        PhotoServiceClient _service;
        Photo _photo;
        public PreviewPhotoViewModel(PhotoServiceClient photoService,Photo photo)
        {
            _photo = photo;
            _service = photoService;
            this.PreviewImageHeight = 510;
            this.PreviewImageWidth = 680;
            this.ImageHorizontalAllignment = HorizontalAlignment.Center;
            this.GetImage(photo);
            
            if (AccessPermission.IsLoggedInUser == true)
                this.IsShowAdminFunctionality = Visibility.Visible;
            else
                this.IsShowAdminFunctionality = Visibility.Collapsed;
        }

        private Visibility _isShowAdminFunctionality;
        public Visibility IsShowAdminFunctionality
        {
            get { return _isShowAdminFunctionality; }
            set
            {
                _isShowAdminFunctionality = value;
                NotifyOfPropertyChange(() => IsShowAdminFunctionality);
            }
        }

        private BitmapImage _imageToDispaly;
        public BitmapImage ImageToDisplay
        {
            get { return _imageToDispaly; }
            set
            {
                _imageToDispaly = value;
                NotifyOfPropertyChange(() => ImageToDisplay);
            }
        }

        private void GetImage(Photo photo)
        {
            var func=Observable.FromEvent<GetPhotoCompletedEventArgs>(_service,"GetPhotoCompleted")
                .ObserveOn(Scheduler.ThreadPool);

            _service.GetPhotoAsync(photo);
            func.ObserveOn(Scheduler.Dispatcher).Select(r => r.EventArgs.Result)
                .Subscribe(s => this.ParsePhoto(s));
        }
        private void ParsePhoto(Photo photo)
        {
            this.ImageToDisplay = HelperMethods.ConvertByteArrayToBitMapImage(photo.ImageFile);
            _photo.ImageFile = photo.ImageFile;
        }

        CloseDelegate _closeDelegate;
        public CloseDelegate PopupCloseDelegate
        {
            get { return _closeDelegate; }
            set { _closeDelegate = value; }
        }

        public void ClosePreview()
        {
            this._closeDelegate();
        }

        public void DeletePhoto()
        {
            _service.DeletePhotoAsync(this._photo);
            Message.EventAggregationProvider.EventAggregator.Publish<Message.CloseMessage>(new Message.CloseMessage());
            Message.EventAggregationProvider.EventAggregator.Publish<Message.RefreshPhotoPreviewMessage>(new Message.RefreshPhotoPreviewMessage(_photo) {Sender=this });
        }

        private int _prevImageheight;
        public int PreviewImageHeight
        {
            get { return _prevImageheight; }
            private set
            {
                _prevImageheight = value;
                NotifyOfPropertyChange(() => PreviewImageHeight);
            }
        }

        private int _prevImageWidth;
        public int PreviewImageWidth
        {
            get { return _prevImageWidth; }
            private set
            {
                _prevImageWidth = value;
                NotifyOfPropertyChange(() => PreviewImageWidth);
            }
        }

        private HorizontalAlignment _imageHorizontalAllignment;
        public HorizontalAlignment ImageHorizontalAllignment
        {
            get { return _imageHorizontalAllignment; }
            private set
            {
                _imageHorizontalAllignment = value;
                NotifyOfPropertyChange(() => ImageHorizontalAllignment);
            }
        }

        private object _imageRelatedInfo;
        public object ImageRelatedInfo
        {
            get { return _imageRelatedInfo; }
            private set
            {
                _imageRelatedInfo = value;
                NotifyOfPropertyChange(() => ImageRelatedInfo);
            }
        }
        private int _imageInfoheight;
        public int ImageInfoHeight
        {
            get { return _imageInfoheight; }
            private set
            {
                _imageInfoheight = value;
                NotifyOfPropertyChange(() => ImageInfoHeight);
            }
        }

        private int _imageInfoWidth;
        public int ImageInfoWidth
        {
            get { return _imageInfoWidth; }
            private set
            {
                _imageInfoWidth = value;
                NotifyOfPropertyChange(() => ImageInfoWidth);
            }
        }

        private bool _isCommentVisible = false;
        public void ShowHideComments()
        {
            if (_isCommentVisible == false)
            {
                CreateSpaceForRelatedInfo();
                this.ImageRelatedInfo = new PublicCommentsViewModel(_service, _photo);
                _isCommentVisible = true;
            }
            else
            {
                this.HideSpaceForRelatedInfo();
                _isCommentVisible = false;
            }
        }

        private void CreateSpaceForRelatedInfo()
        {
            this.PreviewImageHeight = 90;
            this.PreviewImageWidth = 90;
            this.ImageHorizontalAllignment = HorizontalAlignment.Left;
            this.ImageInfoHeight = 430;
            this.ImageInfoWidth = 680;
        }

        private void HideSpaceForRelatedInfo()
        {
            this.PreviewImageHeight = 510;
            this.PreviewImageWidth = 680;
            this.ImageInfoHeight = 0;
        }
    }
}
